<!DOCTYPE html>
<html>
<head>
    <script type="text/javascript" src=".././static/js/public-head.js"></script>
</head>
<body >
<form class="layui-form" action="javascript:;" lay-filter="rpt-form"  >
    <input type="hidden" lay-submit lay-filter="down2007">
    <div class="layui-form-item">
        <div class="layui-inline">
            <button class="layui-btn" lay-submit  id="rpt-query"  lay-filter="rpt-query">查询</button>
        </div>
    </div>
    <hr>
</form>
</body>
<script type="text/javascript">
    layui.config({
        base: basePath+'/layui/lay/modules/'
    }).extend({
        formSelects: 'formSelects-v4'
    }).use([ 'table','form','formSelects' ,'element','laydate','laytpl','layedit','util','code','upload'], function() {
        var $ = layui.$;
        var form = layui.form;
        var formSelects = layui.formSelects;
        var layer = layui.layer;
        var table = layui.table;
        var element = layui.element;
        var laydate= layui.laydate;
        var laytpl = layui.laytpl;
        var layuiutil = layui.util
        var layedit = layui.layedit;
        var upload = layui.upload
        var panelId
        var indexLoad = layer.load();
        var tableOption
        $.getJSON(basePath+"/xdWidgetPanel/viewPanelTableLast"+urlParam,function(rst){
            layer.close(indexLoad)
            if(rst.code !=0){
                layer.alert("异常:<br>"+rst.msg,{icon:2})
                return
            }
            var allSelect = {} //所有的select
            var eventMap = {}
            $.each(rst.formItem,function(index,item){
                $("#rpt-query").before(item.html)
                if(item.classFlag == "xd-date"){
                   laydate.render({elem:"#"+item.name,type:item.type,value: new Date(commonUtil.setDefaultDate(item.defaultValue)),theme:item.theme})
                }else if(item.classFlag =="xd-select" ){
                    allSelect[item.name] = item
                    if(item.events!=null && item.events !== ""){
                        eventMap[item.name] = item
                    }
                }
            })
            viewPanelTableUtil.initSelect(formSelects ,allSelect,eventMap)
            form.render('checkbox');
            form.render('select');
            form.render('radio');
            $("body").append(rst.panel)
            tableOption = rst.table
            panelId = rst.panelId
            tableOption.done = function(res, curr,count){
                if(!tableOption.page){
                    commonUtil.mergeCell(tableOption.id,tableOption.mergeField)
                }
                viewPanelTableUtil.upLoadXLSX(panelId,upload,tableOption.id)
                layer.close(indexLoad)
            }
            if(tableOption.queryFlag){
                table.render(tableOption)
            }
            if(tableOption.autoFlag){
                setInterval(function(){
                    table.render(tableOption)
                },(1000*60) * tableOption.autoInterval );
            }
            table.on("toolbar("+tableOption.id+")",function(obj){
                var event = obj.event;
                var checkStatus = table.checkStatus(obj.config.id);
                var checkCount = checkStatus.data.length
                switch (event){
                    case "downLoad":
                        $("[lay-filter=down2007]").click()
                        break;
                    case "downLoadTmp":
                        //console.log(event,panelId)
                        viewPanelTableUtil.downLoadTmp(panelId)
                        break;
                    case "del":
                        if(checkCount <=0){
                            layer.msg("请先选择一条记录后再操作",{icon:5})
                        }else{
                            layer.confirm("确定要删除该记录吗？",{icon:3,title:tableOption.title},function(index){
                                layer.close(index);
                                var param = checkStatus.data[0]
                                param.widgetPanelId = tableOption.widgetPanelId
                                $.getJSON(basePath+"/xdWidgetPanel/del",param,function(rst){
                                    if(rst.code !=0){
                                        layer.alert(rst.msg,{icon: 1},function(index1){
                                            layer.close(index1)
                                        })
                                    }else{
                                        layer.msg(rst.msg)
                                        table.render(tableOption)
                                    }
                                })
                            })
                        }
                        break;
                    case "add":
                        commonUtil.openPage("新增："+tableOption.title,basePath+"/xdWidgetPanel/add?widgetPanelId="+ tableOption.widgetPanelId )
                        break;
                    case "edit":
                        if(checkCount <=0){
                            layer.msg("请先选择一条记录后再操作",{icon:5})
                        }else{
                            var param = checkStatus.data[0]
                            param.widgetPanelId = tableOption.widgetPanelId
                            var jsonStr = encodeURIComponent(JSON.stringify(param))
                            commonUtil.openPage("编辑："+tableOption.title,basePath+"/xdWidgetPanel/edit?jsonStr="+ jsonStr )
                        }
                        break;
                }
            })
        })
        form.on("submit(down2007)",function(obj){
            indexLoad =  layer.load()
            var url = basePath+"/xdWidgetPanel/createExcel2007?panelId="+panelId ;
            $.post(url,obj.filed,function(rst){
                layer.close(indexLoad)
                //console.log(rst)
                if(rst.code == 0){
                    window.open(basePath+"/specialDownLoad/doXLSXByTempFileAndFileName?fileName="+rst.data.fileName +"&tempFile="+rst.data.tempFile)
                }else{
                    layer.alert("下载异常：<br>"+rst.msg,{icon:2})
                }
            },"json")
        })
        form.on("submit(rpt-query)", function (obj) {
            indexLoad =  layer.load()
            tableOption.where = obj.field
            table.render(tableOption)
        })
    });
var viewPanelTableUtil = {
    upLoadXLSX:function(panelId,upload,layid){
        var layindex
        var upDom =   $("div[lay-id="+layid+"] .layui-table-tool-temp").find("button[lay-event=upLoad]")
        upload.render({elem:upDom,
            url:basePath+"/specialUpLoad/doUpdLoadXdPanelDataByXLSX",
            data:{panelId:panelId},
            accept:"file",
            exts:"xlsx",
            acceptMime:"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
            before: function(obj){ //obj参数包含的信息，跟 choose回调完全一致，可参见上文。
                layindex = layer.load()
            },
            done:function(res){
                layer.closeAll()
                //console.log(res)
                if(res.code !=0){
                    layer.alert(res.msg)
                }else{
                    layer.msg("文件上传成功，正在写入表...",{icon:6},function(){
                        layer.load()
                        $.getJSON(basePath+"/specialUpLoad/importXlsx2TableByPanelId",{tempFile:res.data,panelId:panelId},function(rst){
                            layer.alert(rst.msg,{icon: 6},function(){
                                layer.closeAll()
                            })
                        })
                    })
                }
            },
            error:function(){
                layer.alert("上传文件异常，请重试",function(){
                    layer.closeAll()
                })
            }
        })
    },
    downLoadTmp:function(panelId){
        var indexLoad =  layer.load()
        var url = basePath+"/xdWidgetPanel/createExcelTmp?panelId="+panelId ;
        $.getJSON(url,function(rst){
            layer.close(indexLoad)
            if(rst.code == 0){
                window.open(basePath+"/specialDownLoad/doXLSXByTempFileAndFileName?fileName="+rst.data.fileName +"&tempFile="+rst.data.tempFile)
            }else{
                layer.alert("下载异常：<br>"+rst.msg,{icon:2})
            }
        })
    },
    initSelect:function(formSelects,allSelect,eventMap){
        var existsSelect = []
        $.each(allSelect,function(key,item){
            if(item.serverFlag){
                formSelects.data(item.name, 'server', {
                    type:"post",
                    data:{id:item.id},
                    searchUrl:basePath+"/xdWidgetSelect/selectView",
                    success: function(id, url, searchVal, result){
                    }
                });
            }else{
                formSelects.render(item.name)
            }
        })
        $.each(eventMap,function(eventSelect,item){
            formSelects.on(eventSelect, function(id, vals, val, isAdd, isDisabled){
                var valueAarray = {}
                $.each(item.events.split(";"),function(index,targetSelect){
                    var eventValue = formSelects.value(eventSelect , 'valStr' )
                    valueAarray[eventSelect] = eventValue
                    if(allSelect[targetSelect] != null){
                        if(allSelect[targetSelect].serverFlag){
                            valueAarray.id = allSelect[targetSelect].id
                            formSelects.config(targetSelect, {
                                data:valueAarray
                            }).data(targetSelect, 'server', {
                                url:basePath+"/xdWidgetSelect/selectView"
                            });
                        }else{
                            formSelects.search(targetSelect, eventValue);
                        }
                    }
                })
            },true);
        })
    }
}
</script>
